<?php
Registry::get('tpl')->assign("action", $action);
switch( $action )
{
	case 'save':
		if( $ClientUnit && $ClientName && $ClientPhone && $ClientEmail && $ClientAddr )
		{
			if( $ClientPages )
			{
				list($ClientPagesId, $ClientPagesName) = explode("|", $ClientPages);
			}
			
			$sql = "INSERT INTO aams_client(cunit,cuid,cpages_id,cpages_name,cname,cphone,cemail,caddr,cwebsite,clogo,cintro,ctime) 
							VALUES('$ClientUnit', '$uid', '$ClientPagesId', '$ClientPagesName', '$ClientName', '$ClientPhone',
							'$ClientEmail', '$ClientAddr', '$ClientWebsite', '$ClientLogo', '$ClientIntro', NOW())";
			if( $_db->Execute($sql) == true )
			{
				fn_set_notification('S', '你已經成功新增主辦單位 '.$ClientUnit, 'client.html');
			}else
				fn_alertMsg('系統發生異常，請洽客服人員');
		}else
			fn_alertMsg('系統發生異常，請洽客服人員', 'client.html');
		exit;
	break;
	case 'create':
		Registry::get('tpl')->assign("FBPages", fn_loadFacebookPages($uid));
		Registry::get('tpl')->assign("main", "dashboard/client_create.html");
		Registry::get('tpl')->display("template.html"); 	
	break;
	case 'edit':
		if( $cid )
		{
			$sql = "SELECT * FROM aams_client WHERE cid='$cid'";
			$aryData = $_db->GetRow($sql);
			Registry::get('tpl')->assign("aryData", $aryData);
			Registry::get('tpl')->assign("FBPages", fn_loadFacebookPages($uid));
			Registry::get('tpl')->assign("main", "dashboard/client_create.html");
			Registry::get('tpl')->display("template.html"); 	
		}else
			header("location: /dashboard/client.html");
	break;
	default:
		$fb_pages = fn_loadFacebookPages($uid);
		if( sizeof($fb_pages) > 0 ){
			foreach( $fb_pages as $k => $v )
			{
				$fb_pages_id_ary[] = $v['id']; 	
			} 
			$sql_extra = ' OR c.cpages_id IN('.implode(",", $fb_pages_id_ary).')';
		}
		$sql = "SELECT c.cid, c.cunit, c.cuid, c.ctime, u.realname FROM aams_client as c 
						LEFT JOIN aams_user as u ON (u.uid=c.cuid) 
						WHERE u.uid = '$uid' $sql_extra
						ORDER BY c.cid ASC";
		$aryData = $_db->GetAll($sql);
		Registry::get('tpl')->assign("aryData", $aryData);
		Registry::get('tpl')->assign("main", "dashboard/client_list.html");
		Registry::get('tpl')->display("template.html"); 	
	break;	
}

?>